package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

public class Level {
	int width;
	int height;
	int stageNow=1;
	static final int MAXLEVEL =4;
	Vector2 positionBob = new Vector2(0,0);
	Vector2 positionDoor = new Vector2(0,0);
	Block [][] blocks;
	Array<String> musicOfLevel = new Array<String>();
	public int getWidth(){
		return width;
	}
	public void setWidth(int wid){
		width=wid;
	}
	public int getHeight(){
		return height;
	}
	public void setHeight(int hei){
		height=hei;
	}
	public Block[][] getBlock(){
		return blocks;
	}
	public void setBlock (Block [][] block){
		this.blocks=block;
	}
	public Block get(int x, int y){
		return blocks[x][y];
	}
	public Vector2 getPositionBob(){
		return positionBob;
	}
	public Vector2 getPositionDoor(){
		return positionDoor;
	}
	public void setStage(int stage){
		stageNow=stage;
		
	}
	public int getStageNow(){
		return stageNow;
	}
	private void loadLevelFromFile(String path) throws IOException{
		FileHandle file = Gdx.files.internal(path);
		BufferedReader bufReader = file.reader(1024);
		Scanner in = new Scanner(bufReader);
		try{
			
			width = in.nextInt();
			height = in.nextInt();
			blocks = new Block[width][height];
			
			
			int t,x,y;
			do{
				t=in.nextInt();
				x = in.nextInt();
				y = in.nextInt();
				if(x!=-1&&y!=-1){
					switch(t){
						case 0: positionBob.set(x,y); break;
						case 1: blocks[x][y]=new Block(new Vector2(x,y));break;
						case 2: positionDoor.set(x, y); break;
					}
					
				}
			}while(x!=-1&&y!=-1);
			
		}
		catch(Exception e)
		{}
	}
	private void loadDemolevel(){
		width =10;
		height=7;
		blocks = new Block[width][height];
		for(int col =0 ; col<width;col++)
		{
			for(int row = 0 ;row<height;row++){
				blocks[col][row]= null;
			}
		}
		for(int col =0; col<width;col++){
			blocks[col][0] =new Block(new Vector2(col,0));
			blocks[col][6]= new Block(new Vector2(col,6));
			if(col>2)
				blocks[col][1]=new Block(new Vector2(col,1));
		}
		for(int row =0 ; row<height;row++){
			blocks[0][row] =new Block(new Vector2(0,row));
		}
		

         blocks[6][3] = new Block(new Vector2(6, 3));
         blocks[6][4] = new Block(new Vector2(6, 4));
         blocks[6][5] = new Block(new Vector2(6, 5));
         
         blocks[3][2] = new Block(new Vector2(3, 2));
         blocks[3][3] = new Block(new Vector2(3, 3));
         
         blocks[1][1] = new Block(new Vector2(1, 1));
         blocks[1][2] = new Block(new Vector2(1, 2));
	}
	public Level(){
		musicOfLevel.add("music/level1.mp3");
		musicOfLevel.add("music/level2.mp3");
		musicOfLevel.add("music/level3.mp3");
		musicOfLevel.add("music/level4.mp3");
	}
	public String getMusic(){
		return musicOfLevel.get(stageNow-1);
		
	}
	public void setLevel(int i){
		if(i==0)
			loadDemolevel();
		
			try {
				loadLevelFromFile("maps/map"+i+".txt");
				stageNow=i;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public boolean nextLevel(){
		stageNow++;
		if(stageNow<=MAXLEVEL)
			try {
				loadLevelFromFile("maps/map"+stageNow+".txt");
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		else{
			return false;
		}
	}
	public void resetLevel(){
		stageNow=1;
	}
}
